<template>
  <div class="cart-container">
    <h2>我的购物车</h2>

    <div v-if="cartStore.loading" class="loading">
      加载中...
    </div>

    <template v-else>
      <div v-if="cartStore.items.length === 0" class="empty-cart">
        <i class="fas fa-shopping-cart"></i>
        <p>购物车是空的</p>
        <router-link to="/mall" class="go-shopping">
          去购物
        </router-link>
      </div>

      <template v-else>
        <!-- 购物车列表 -->
        <div class="cart-list">
          <div v-for="item in cartStore.items" :key="item.product._id" class="cart-item">
            <div class="item-image">
              <img :src="item.product.images[0]" :alt="item.product.name">
            </div>

            <div class="item-info">
              <router-link 
                :to="`/mall/product/${item.product._id}`"
                class="item-name"
              >
                {{ item.product.name }}
              </router-link>
              
              <div class="item-price">
                <template v-if="item.product.discount > 0">
                  <span class="discounted-price">
                    ¥{{ (item.product.price * (1 - item.product.discount / 100)).toFixed(2) }}
                  </span>
                  <span class="original-price">
                    ¥{{ item.product.price }}
                  </span>
                </template>
                <span v-else class="price">
                  ¥{{ item.product.price }}
                </span>
              </div>
            </div>

            <div class="item-quantity">
              <button 
                @click="updateQuantity(item.product._id, item.quantity - 1)"
                :disabled="item.quantity <= 1"
              >-</button>
              <span>{{ item.quantity }}</span>
              <button 
                @click="updateQuantity(item.product._id, item.quantity + 1)"
                :disabled="item.quantity >= item.product.stock"
              >+</button>
            </div>

            <div class="item-subtotal">
              ¥{{ calculateSubtotal(item).toFixed(2) }}
            </div>

            <button 
              class="remove-item"
              @click="removeItem(item.product._id)"
            >
              删除
            </button>
          </div>
        </div>

        <!-- 购物车底部 -->
        <div class="cart-footer">
          <div class="cart-summary">
            <div class="summary-item">
              <span>商品总数:</span>
              <span>{{ cartStore.totalItems }} 件</span>
            </div>
            <div class="summary-item total">
              <span>总计:</span>
              <span>¥{{ cartStore.totalAmount.toFixed(2) }}</span>
            </div>
          </div>

          <div class="cart-actions">
            <button 
              class="clear-cart"
              @click="clearCart"
            >
              清空购物车
            </button>
            <button 
              class="checkout"
              @click="handleCheckout"
            >
              去结算
            </button>
          </div>
        </div>
      </template>
    </template>
  </div>
</template>

<script setup lang="ts">
import { useRouter } from 'vue-router'
import { useCartStore } from '@/stores/cart'
import type { CartItem } from '@/types'

const router = useRouter()
const cartStore = useCartStore()

// 计算单个商品小计
const calculateSubtotal = (item: CartItem) => {
  const price = item.product.price * (1 - item.product.discount / 100)
  return price * item.quantity
}

// 更新商品数量
const updateQuantity = async (productId: string, quantity: number) => {
  await cartStore.updateQuantity(productId, quantity)
}

// 移除商品
const removeItem = async (productId: string) => {
  if (confirm('确定要删除这个商品吗？')) {
    await cartStore.removeFromCart(productId)
  }
}

// 清空购物车
const clearCart = async () => {
  if (confirm('确定要清空购物车吗？')) {
    await cartStore.clearCart()
  }
}

// 去结算
const handleCheckout = () => {
  router.push('/mall/checkout')
}
</script>

<style scoped>
.cart-container {
  padding: 2rem;
  max-width: 1200px;
  margin: 0 auto;
}

.cart-container h2 {
  margin-bottom: 2rem;
  color: #333;
}

.loading {
  text-align: center;
  padding: 2rem;
  color: #666;
}

.empty-cart {
  text-align: center;
  padding: 4rem 0;
  color: #666;
}

.empty-cart i {
  font-size: 4rem;
  margin-bottom: 1rem;
}

.go-shopping {
  display: inline-block;
  margin-top: 1rem;
  padding: 0.5rem 2rem;
  background-color: #007bff;
  color: white;
  text-decoration: none;
  border-radius: 4px;
}

.cart-list {
  margin-bottom: 2rem;
}

.cart-item {
  display: grid;
  grid-template-columns: auto 1fr auto auto auto;
  gap: 2rem;
  align-items: center;
  padding: 1rem;
  border-bottom: 1px solid #eee;
}

.item-image {
  width: 100px;
  height: 100px;
}

.item-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 4px;
}

.item-info {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.item-name {
  color: #333;
  text-decoration: none;
  font-weight: 500;
}

.item-price {
  color: #ff4444;
}

.original-price {
  color: #999;
  text-decoration: line-through;
  margin-left: 0.5rem;
  font-size: 0.9rem;
}

.item-quantity {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.item-quantity button {
  width: 30px;
  height: 30px;
  border: 1px solid #ddd;
  background: white;
  cursor: pointer;
}

.item-quantity button:disabled {
  background-color: #f5f5f5;
  cursor: not-allowed;
}

.item-subtotal {
  color: #ff4444;
  font-weight: bold;
}

.remove-item {
  padding: 0.5rem 1rem;
  border: none;
  background-color: #dc3545;
  color: white;
  border-radius: 4px;
  cursor: pointer;
}

.cart-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 1rem;
  background-color: #f8f9fa;
  border-radius: 4px;
}

.cart-summary {
  display: flex;
  gap: 2rem;
}

.summary-item {
  display: flex;
  gap: 0.5rem;
}

.summary-item.total {
  font-size: 1.2rem;
  color: #ff4444;
  font-weight: bold;
}

.cart-actions {
  display: flex;
  gap: 1rem;
}

.cart-actions button {
  padding: 0.75rem 2rem;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.clear-cart {
  background-color: #6c757d;
  color: white;
}

.checkout {
  background-color: #ff4444;
  color: white;
}
</style> 